<?php

class AdminAction extends Action
{

    public function index()
    {
        //dump($_SESSION['UserInfo']);
        $this->display('Public:login');
    }
    //用户注册提交的方法url
    public function insert()
    {
        header('Content-Type:text/html; charset=utf-8'); //防止出现乱码
        if (empty($_POST['user_id']))
        {
            $this->error('帐号错误！');
        } elseif (empty($_POST['password']))
        {
            $this->error('密码必须！');
        } elseif (empty($_POST['verify']))
        {
            $this->error('验证码必须！');
        } elseif ($_SESSION['verify'] != md5($_POST['verify']))
        {
            $this->error('验证码错误！');
        }
        $boss = $_POST['boss'];
        $User = D('User');
        if ($_POST['kind'] == 'Conductor')
        {
            $bossid = $User->where('user_id = ' . "'" . $boss . "'" .
                " AND kind != 'Conductor' AND kind != 'Guest' ")->find();
            if (!$bossid)
            {
                $this->error('客服指定的老板id不存在');
            }
        }
        {
            $uid = $User->where('user_id = ' . "'" . $_POST['user_id'] . "'")->find();
            if ($uid)
            {
                $this->error('注册的id已经存在');
            }
        }

        //$Pagemodel = D("user");
        $vo = $User->create();
        if (false === $vo)
            die($User->getError());
        $topicid = $User->add(); //add方法会返回新添加的记录的主键值
        if ($topicid)
        {
            $role = $User->where('id_tb_user = ' . $topicid)->find();
            Session::set('UserInfo', $role);

            $url = __APP__;
            header("Location: " . $url);
        } else
            $this->error("<script>alert('数据库添加失败');history.back();</script>");
    }

    //产生验证码
    public function verify()
    {

        import("@.ORG.Image");
        import("@.ORG.String");
        Image::buildImageVerify();
    }

    // 登录检测
    public function checkLogin()
    {   
        if (empty($_POST['user_id']))
        {
            $this->error('帐号错误！');
        } elseif (empty($_POST['password']))
        {
            $this->error('密码必须！');
        } elseif (empty($_POST['verify']))
        {
            $this->error('验证码必须！');
        } elseif ($_SESSION['verify'] != md5($_POST['verify']))
        {
            $this->error('验证码错误！');
        }
        
        //dump($_POST['checked']);

        //检查登陆id和捆绑的密码对应记录是否存在
        $userid = $_POST['user_id'];
        $password = $_POST['password'];
        $User = D('User');
        $role = $User->where('user_id =' . '"' . $userid . '"' . ' and password =' . '"' .
            $password . '"')->find();
        $ncd =     getcwd();        
        //$savePath = realpath($savePath);         
        //dump($savePath);  
        if($role)
        {
            Session::set('UserInfo', $role); 
            $savePath = $ncd . '\Public\Uploads\\' . $_SESSION['UserInfo']['boss'];          
           
            if (!is_dir($savePath) and (!file_exists($savePath) ))
            {   
                if (!mkdir($savePath))
                {
                    $this->error = '用户目录' . $savePath . '建立错误';                    
                }
            }
            
            if ($_POST['checked'] == 'true')
            {
                setcookie('keepLogin', 'true', time() + 3600 * 24 * 7, '/');
                setcookie('password', $_POST['password'], time() + 3600 * 24 * 7, '/');
                setcookie('user_id', $_POST['user_id'], time() + 3600 * 24 * 7, '/');
            }

            $url = __APP__;
            header("Location: $url");
            //echo 'hi';
            //dump('9');
            //$this->display('Index:index');
        } else
        {
            $this->error('登陆出错~');
        }

    }

    public function ajaxLogin()
    {


        $this->display('Public:alogin');

    }

    public function _ajaxLogin()
    {

        $user_id = $_POST['userid'];
        $password = $_POST['password'];
        $User = D('User');
        $role = $User->where("user_id = '$user_id' and password ='$password'")->find();
        if ($role)
        {
            if (isset($_POST['loginKind']) and ($_POST['loginKind'] == 'count_person'))
            {
                Session::set('count_person', $role);
                setcookie('count_person', $userid, 3600);
                setcookie('count_person', $userid, time() + 3600, '/');                
            } else
                Session::set('UserInfo', $role);
	            echo ajaxrt('ok','登陆ok',$role);
        } else
        {
			echo ajaxrt('error','登陆出错',$role);
        }

    }

    public function regUser()
    {

        $this->display('Public:reg');

    }

    //退出登录
    public function logout()
    {

        Session::clear();
        setcookie("keepLogin", 'false', time() - 3600,'/');
        setcookie("user_id", '', time() - 3600,'/');
        
        setcookie("password", "", time() - 3600,'/');
        $url = __ROOT__ . '/index.php/Admin';
        header("Location: " . $url);
    }
    
    //提交mac，获得sn页面
    public function getSn(){
        
      $this->display('Public:getSn');  
    }
    
    //显示序列号
    public function ShowSn(){

         if(isset($_GET['sn']) AND ($_GET['sn'] !='') )
         {echo "<pre>您序列号是".$_GET['sn']."<BR/>仅适用于本台电脑</pre>";}
         else
         {
            echo "<pre>您序列号是:".$_COOKIE["__SN"]."<BR/>仅适用于本台电脑</pre>";
         }
         
     
    }
    
    public function entSn()     
    {     
           //$svPdo =D('user');
            //$rt = $svPdo->where("MAC = '$mac'")->find();
            
            $this->display('Public:enterSn');
          
     } 
    
    //此函数只存在于Server版
    public function  MakeSN(){
        
    if(isset($_GET['id']) and isset($_GET['mail'])  and isset($_GET['callback']))
    {
        $id =$_GET['id'];$mail =$_GET['mail'] ;$mac =$_GET['mac'];$callback = $_GET['callback'];
        $ar = explode('-',$mac);        
        $str = '';
        for($i=0;$i<count($ar);$i++)
        {
            $str = $str.$ar[$i];
        }
        $sn = md5(bin2hex($str.'jhfnetboy'));

        //mail 给用户sn
        $to = 'jhfnetboy@sina.com';
        $subject = "有用户 ：".$id."，MAC为 :".$mac." ,申请序列号到：".$mail;
        $body = "序列号为：".$sn.";".$subject;
        ini_set ("sendmail_from","jhfnetboy@gamil.com");
        if (mail($to, $subject, $body)) {
                 $rt = array('status'=>'ok','msg'=>"<p>请24小时内查收".$mail."!</p>",'data'=>$sn);
                 echo  $callback.'('.json_encode($rt).')';
            } 
        else {$rt = array('status'=>'error','msg'=>"抱歉，申请失败,请重新申请",'data'=>NULL);
         echo  $callback.'('.json_encode($rt).')';
        }

           //下面几句是本地url测试用的 
//        $rt = array('status'=>'ok','msg'=>"申请成功".$sn,'data'=>$sn);        
//        echo  $callback.'('.json_encode($rt).')';
            
      }
    else
    {echo $callback."('数据不规范');";}     
    }
    
//此函数只存在于Server版,检测sn是否符合
public function chkSN(){
   if(isset($_GET['sn']) and isset($_GET['mac'])  and isset($_GET['callback']))
    {
        $mac =trim($_GET['mac']); $sn =$_GET['sn'];
        $callback = $_GET['callback'];  
    
    $snPdo = D('Server_user_valid');
    
    $rt = $snPdo->where("sn ='".$sn ."' and  MAC = '".$mac."'")->find();              
        if(count($rt)>0)
        {
          $rtt = array('status'=>'ok','msg'=>"序列号正确",'data'=>$sn);  
          echo  $callback.'('.json_encode($rtt).')';
        }
        else
        {
        $rtt = array('status'=>'error','msg'=>"序列号错误",'data'=>NULL);
         echo  $callback.'('.json_encode($rtt).')';   
        }        
    }
  }  
    
 public function upUser(){
    $HAS = $_POST['HAS_SN'];
    $sn =$_POST["sn"]; 
    //$mac =  $_SESSION['UserInfo']['mac'];
    $mac = $_COOKIE["__MAC"];
    $uPdo = D('user'); 
  $rt = $uPdo->execute("UPDATE `tb_user` SET `HAS_SN` = $HAS WHERE mac = '$mac'");
  setcookie('HAS_SN', 1, time() + 3600 * 24 * 7, '/');
  $rtt = $uPdo->execute("UPDATE `tb_user` SET `sn` = '$sn' WHERE mac = '$mac'");
  echo ajaxrt('ok',$sn);
 } 

}

?>